package com.spring.sunflower.conversation

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.recyclerview.widget.RecyclerView
import com.spring.sunflower.bottompanel.EmotionUtils
import com.spring.sunflower.bottompanel.EmotionUtils.EMOTION_CLASSIC_TYPE
import com.spring.sunflower.bottompanel.GlobalOnItemClickManagerUtils
import com.spring.sunflower.util.DisplayUtils

/**
 * Created by Kevin on 2021/7/30<br/>
 *
 * Describe:<br/>
 */
class EmotionVerticalAdapter(var context: Context, var data: List<String>) :
    RecyclerView.Adapter<EmotionVerticalAdapter.ViewHolder>() {
    private val screenWidth = DisplayUtils.getScreenWidth(context)

    // item的间距
    val spacing = DisplayUtils.dip2px(context, 20f)
    private val itemWidth = (screenWidth - spacing * 8) / 7

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        //        val ivEmotion = itemView.findViewById<ImageView>(R.id.ivEmotion)
        val ivEmotion: ImageView = itemView as ImageView
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): EmotionVerticalAdapter.ViewHolder {
        var imageView = ImageView(context)
        val layoutParams = RelativeLayout.LayoutParams(itemWidth, itemWidth)
        layoutParams.topMargin = spacing
        imageView.layoutParams = layoutParams
        return ViewHolder(imageView)
    }

    override fun onBindViewHolder(holder: EmotionVerticalAdapter.ViewHolder, position: Int) {
//        holder.ivEmotion.setImageResource(
//            EmotionUtils.getImgByName(
//                EMOTION_CLASSIC_TYPE,
//                data[position]
//            )
//        )
        holder.ivEmotion.setImageBitmap(
            EmotionUtils.getImageByName(
                EMOTION_CLASSIC_TYPE,
                data[position]
            )
        )
        holder.ivEmotion.setOnClickListener {
            GlobalOnItemClickManagerUtils.getInstance(context)
                .onEmotionItemClickListener(EMOTION_CLASSIC_TYPE, data, position)
        }

    }

    override fun getItemCount(): Int {
        return data.size
    }
}